package com.yunji.base.task;

import com.yunji.base.domain.BelongDO;
import com.yunji.base.domain.ManagerDO;
import com.yunji.base.domain.SiteScaleDO;
import com.yunji.base.enums.ScaleTypes;
import com.yunji.base.service.BelongService;
import com.yunji.base.service.ManagerService;
import com.yunji.base.service.SiteService;
import com.yunji.common.model.domain.SiteDO;
import com.yunji.common.utils.ConvertDataUtil;
import com.yunji.common.utils.DecimalUtils;
import com.yunji.common.utils.TimeUtils;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;

/**
 * @Project: yunji-coffee
 * @Package: com.yunji.user.task
 * @Title: BelongTask
 * <p>
 * History:
 * Date                     Version     Author          Summary
 * ============================================================
 * 2024-11-25 17:54:38      V1.0        HuaAo       新建类
 */
@Component("belongTask")
public class BelongTask {

    @Resource
    SiteService siteService;

    @Resource
    ManagerService managerService;

    @Resource
    BelongService belongService;

    /**
     * @Title siteScale
     * @Desc 网点按年限分润
     * @Date 2024-11-25 13:45:09.633
     */
    @Transactional
    public void siteScale() {
        // 找到按年限分润的网点 根据时间进行分润
        List<BelongDO> belongList = belongService.getByScaleType(ScaleTypes.CONTRACT.getCode());
        List<ManagerDO> modifyManagerList = new ArrayList<>();
        List<BelongDO> modifyBelongList = new ArrayList<>();
        for (BelongDO b : belongList) {
            ManagerDO manager = new ManagerDO();
            BelongDO belong = new BelongDO();
            Long siteId = b.getSiteId();
            SiteDO site = siteService.getById(siteId);
            List<SiteScaleDO> scaleList = siteService.listScaleBySiteId(siteId);
            // 转化按年分润Map
            Map<Integer, BigDecimal> scaleMap = ConvertDataUtil.list2KeyValueMap(scaleList, SiteScaleDO::getScaleYear, SiteScaleDO::getScaleRatio);
            // 查看是第几年合同分润
            Date scaleDateS = site.getSiteScaleDates();
//            int i = TimeUtils.differentByMillisecond(scaleDateS, TimeUtils.getNowDate(), Calendar.YEAR);
            int i = TimeUtils.differentByMillisecond(scaleDateS, TimeUtils.getNowDate(), Calendar.MINUTE);
            if (scaleDateS.after(TimeUtils.getNowDate())) {
                i = 1;
            } else {
                i = i < 3 ? i + 1 : 4;
            }
            // 年限分润
            BigDecimal contractScale = scaleMap.get(i);
            // 当前分润
            BigDecimal scale = b.getScaleRatio();
            // 分润不同按照合同年限的来
            if (!DecimalUtils.compare(scale, contractScale)) {
                manager.setManagerId(b.getUserId());
                manager.setScale(contractScale);
                belong.setBelongId(b.getBelongId());
                belong.setScaleRatio(contractScale);
                modifyManagerList.add(manager);
                modifyBelongList.add(belong);
            }
        }

        for (ManagerDO manager : modifyManagerList) {
            managerService.updateScaleByManagerId(manager.getId(), manager.getScale());
        }

        for (BelongDO belong : modifyBelongList) {
            belongService.update(belong);
        }
    }

}